<%@ page language="java" pageEncoding="utf8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%--
  ~ Copyright (c) 2013-2015 SimaStudio. All rights reserved.
  ~
  ~ This software is the confidential and proprietary information of SimaStudio.
  ~ You shall not disclose such Confidential Information and shall use it only
  ~ in accordance with the terms of the agreements you entered into with SimaStudio.
  ~
  ~ SimaStudio MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
  ~ EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  ~ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  ~
  ~ SimaStudio SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  ~ MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  --%>

<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/static/css/list_table.css"/>
</head>

<title>task</title>

<body class="bgray">
<form id="addForm" method="post">

    <table class="list_table">
        <thead>
        <tr>
            <td style="width: 20px">id&nbsp;&nbsp;&nbsp;</td>
            <td>name</td>
            <td style="width: 5%">group</td>
            <td style="width: 5%">状态</td>
            <td style="width: 6%">cron表达式</td>
            <td>描述</td>
            <td style="width: 5%">同步否</td>
            <td style="width: 23%">类路径</td>
            <td>spring id</td>
            <td>方法名</td>
            <td style="width: 8%">操作</td>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="job" items="${taskList}">
            <tr>
                <td>${job.id }</td>
                <td>${job.jobName }</td>
                <td>${job.jobGroup }</td>
                <td>
                    <c:choose>
                        <c:when test="${job.status=='1' }">
                            运行中
                        </c:when>
                        <c:otherwise>
                            已停止
                        </c:otherwise>
                    </c:choose>
                </td>
                <td>${job.cronExpression }</td>
                <td>${job.description }</td>
                <td>${job.isConcurrent }</td>
                <td>${job.beanClass }</td>
                <td>${job.beanName }</td>
                <td>${job.methodName }</td>
                <td>
                    <a href="javascript:;" onclick="updateCron('${job.id}')">更新cron</a>
                    <c:choose>
                        <c:when test="${job.status=='1' }">
                            <a href="javascript:;"
                               onclick="changeJobStatus('${job.id}','stop')" style="color: red">停止任务</a>&nbsp;
                        </c:when>
                        <c:otherwise>
                            <a href="javascript:;"
                               onclick="changeJobStatus('${job.id}','start')" style="color: green">启动任务</a>&nbsp;
                        </c:otherwise>
                    </c:choose>
                </td>
            </tr>
        </c:forEach>
        <tr>
            <td>n</td>
            <td><input type="text" name="jobName" id="jobName"></td>
            <td><input type="text" name="jobGroup" id="jobGroup"></td>
            <td>已停止<input type="hidden" name="status" value="0"></td>
            <td><input type="text" name="cronExpression"
                       id="cronExpression"></td>
            <td><input type="text" name="description" id="description"></td>
            <td><select name="isConcurrent" id="isConcurrent">
                <option value="1">1</option>
                <option value="0">0</option>
            </select></td>
            <td><input type="text" name="beanClass" id="beanClass"></td>
            <td><input type="text" name="beanName" id="beanName"></td>
            <td><input type="text" name="methodName" id="methodName"></td>
            <td><input type="button" onclick="add()" value="保存"/></td>
        </tr>
        </tbody>
    </table>
</form>
<script src="${ctx}/static/js/jquery-2.0.3.min.js"></script>
<script>
    function validateAdd() {
        if ($.trim($('#jobName').val()) == '') {
            alert('name不能为空！');
            $('#jobName').focus();
            return false;
        }
        if ($.trim($('#jobGroup').val()) == '') {
            alert('group不能为空！');
            $('#jobGroup').focus();
            return false;
        }
        if ($.trim($('#cronExpression').val()) == '') {
            alert('cron表达式不能为空！');
            $('#cronExpression').focus();
            return false;
        }
        if ($.trim($('#beanClass').val()) == '' && $.trim($('#beanName').val()) == '') {
            $('#beanClass').focus();
            alert('类路径和spring id至少填写一个');
            return false;
        }
//        if ($.trim($('#methodName').val()) == '') {
//            $('#methodName').focus();
//            alert('方法名不能为空！');
//            return false;
//        }
        return true;
    }
    function add() {
        if (validateAdd()) {
            showWaitMsg();
            $.ajax({
                type: "POST",
                async: false,
                dataType: "JSON",
                cache: false,
                url: "${ctx}/task/add",
                data: $("#addForm").serialize(),
                success: function (data) {
                    hideWaitMsg();
                    if (data.flag) {

                        location.reload();
                    } else {
                        alert(data.message);
                    }

                }//end-callback
            });//end-ajax
        }
    }
    function changeJobStatus(id, cmd) {
        $.ajax({
            type: "POST",
            async: false,
            dataType: "JSON",
            cache: false,
            url: "${ctx}/task/changeJobStatus",
            data: {
                id: id,
                cmd: cmd
            },
            success: function (data) {
                if (data.success) {
                    location.reload();
                } else {
                    alert(data.message);
                }

            }//end-callback
        });//end-ajax
    }
    function updateCron(id) {
        var cron = prompt("输入cron表达式！", "")
        if (cron) {
            showWaitMsg();

            $.ajax({
                type: "POST",
                async: false,
                dataType: "JSON",
                cache: false,
                url: "${ctx}/task/updateCron",
                data: {
                    id: id,
                    cron: cron
                },
                success: function (data) {
                    hideWaitMsg();
                    if (data.flag) {

                        location.reload();
                    } else {
                        alert(data.message);
                    }

                }//end-callback
            });//end-ajax
        }

    }
    function showWaitMsg(msg) {
    }
    function hideWaitMsg() {
    }
</script>
</body>
</html>